'use strict';

/**
 * @ngdoc function
 * @name afasApp.controller:CreditcardtypeCtrl
 * @description
 * # CreditcardtypeCtrl
 * Controller of the afasApp
 */

angular.module('afasApp')
  .controller('CreditcardtypesCtrl', ['$scope', '$state', 'Services', function ($scope, $state, Services) {

    $scope.creditcardtypes = [];
    $scope.newItem = {};
    $scope.errors = {};
    $scope.params = {
      'include': 'credit_card_fees'
    };
    $scope.psHistory = false;
    $scope.histories = [];

    function applyRemoteData( newData ) {
      $scope.creditcardtypes = newData;
    }

    function loadRemoteData() {
      Services.getItems($scope.CREDITCARDTYPE_URL, $scope.params)
      .then(function( results ) {
        applyRemoteData( results.data );
      });
    }

    loadRemoteData();

    $scope.add = function () {
      // console.log($scope.newItem);
      $scope.errors = {};
      Services.add($scope.CREDITCARDTYPE_URL, $scope.newItem)
      .then(function(results){
        // console.log(results);
        $scope.newItem = {};
        $state.go( 'creditcardtypes.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.delete = function (item) {
      Services.delete($scope.CREDITCARDTYPE_URL, item);
    };

    $scope.showHistory = function(id){
      $scope.psHistory = true;
      $scope.histories = [];
      var cc = _.filter($scope.creditcardtypes, function(obj){
        return obj.Id === id;
      });
      $scope.histories = cc[0].CreditCardFees;
      // Services.getItem($scope.CREDITCARDTYPE_URL, id, {'include': 'credit_card_fees_all'})
      // .then(function(results){
      //   $scope.histories = results.data.CreditCardFees;
      // })
    };
  }])
  .controller('CreditcardtypeCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.current = {};
    $scope.currentID = $stateParams.id;
    $scope.current = {};
    $scope.newItem = {
      'CreditCardType': {
        'Id': $scope.currentID
      }
    };
    $scope.params = {
      'include': 'credit_card_fees'
    }

    Services.getItem($scope.CREDITCARDTYPE_URL, $stateParams.id, $scope.params)
    .then(function(results){
      $scope.current = results.data;
    });

    $scope.addFee = function () {
      Services.add('credit_card_fees', $scope.newItem)
      .then(function(results){
        if ($scope.current.CreditCardFees.length > 0) {
          $scope.current.CreditCardFees[0] = results.data;
        } else {
          $scope.current.CreditCardFees = [];
          $scope.current.CreditCardFees.push(results.data);
        };
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.update = function () {
      Services.update($scope.CREDITCARDTYPE_URL, $scope.current, $scope.currentID)
      .then(function(results){
        $state.go( '^.show', {id: $scope.currentID} );
      });
    };
  }]);